package com.scommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scommunity.domain.HelpItem;
import com.scommunity.mapper.HelpItemMapper;
import com.scommunity.service.IHelpItemService;
import com.scommunity.utils.ArgsUtils;
import com.scommunity.utils.MinioUtils;
import com.scommunity.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class HelpItemServiceImpl extends ServiceImpl<HelpItemMapper, HelpItem> implements IHelpItemService {

    @Autowired
    private HelpItemMapper helpItemMapper;

    @Autowired
    private ArgsUtils argsUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private MinioUtils minioUtils;


    //    @Cacheable(value = "helpItemList",key = "idGenerator", unless = "#result == null")
    @Override
    public List<HelpItem> listByHelpId(Long helpId) {
        List<HelpItem> helpItemList = redisUtils.get("helpItemList::" + helpId, List.class);
        if (helpItemList == null) {
            helpItemList = helpItemMapper.selectList(new QueryWrapper<HelpItem>().eq(argsUtils.LongIsNotNull(helpId), "help_id", helpId));
            redisUtils.set("helpItemList::" + helpId, helpItemList, 600);
        }
        return helpItemList;
    }

    @Override
    public String upload(MultipartFile file) {
        String itemPic = minioUtils.putObject(file);
        return itemPic;
    }

    @Override
    public List<HelpItem> getList() {
        return helpItemMapper.selectHelpItemList();
    }

}
